package com.baoai.controller;

import com.baoai.common.utils.JsonUtil;
import com.baoai.dto.*;
import com.baoai.service.IndexService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaren on 2017/4/6.
 * 添加索引
 */
@RequestMapping("/searchIndex/index")
@Controller
public class IndexController {

    @Autowired
    private IndexService indexService;


    /**
     * 视频索引
     *
     * @param videoDto
     * @return
     */
    @RequestMapping("/addVideoIndex")
    @ResponseBody
    public Object addVideoIndex(VideoDto videoDto) throws IOException {
        indexService.saveOrUpdate(videoDto);
        return new APIResponse<>();
    }

    /**
     * 课程索引
     *
     * @param courseDto
     * @return
     */
    @RequestMapping("/addCourseIndex")
    @ResponseBody
    public Object addCourseIndex(CourseDto courseDto) throws IOException {
        indexService.saveOrUpdate(courseDto);
        return new APIResponse<>();
    }

    /**
     * 文章索引
     *
     * @param articleDto
     * @return
     */
    @RequestMapping("/addArticleIndex")
    @ResponseBody
    public Object addArticleIndex(ArticleDto articleDto) throws IOException {
        indexService.saveOrUpdate(articleDto);
        return new APIResponse<>();
    }


    /**
     * 广告索引
     *
     * @param advertDto
     * @return
     */
    @RequestMapping("/addAdvertIndex")
    @ResponseBody
    public Object addAdvertIndex(AdvertDto advertDto) throws IOException {
        indexService.saveOrUpdate(advertDto);
        return new APIResponse<>();
    }

    /**
     * 导师索引
     *
     * @param tutorDto
     * @return
     */
    @RequestMapping("/addTutorIndex")
    @ResponseBody
    public Object addTutorIndex(TutorDto tutorDto) throws IOException {
        indexService.saveOrUpdate(tutorDto);
        return new APIResponse<>();
    }

    /**
     * 评论索引
     *
     * @param commentDto
     * @return
     */
    @RequestMapping("/addCommentIndex")
    @ResponseBody
    public Object addCommentIndex(CommentDto commentDto) throws IOException {
        indexService.saveOrUpdate(commentDto);
        return new APIResponse<>();
    }

    /**
     * 公共查询
     *
     * @param searchText
     * @param className
     * @param sex
     * @param channel
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryCommonList")
    @ResponseBody
    public Object queryCommonList(String searchText, String className, String sex, String channel, int pageIndex, int pageSize, String stateArray) throws IOException {
        if(StringUtils.isEmpty(stateArray)){  // 默认是查询发布状态
            stateArray = "1";
        }
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        List<Object> objectList = indexService.queryCommonList(searchText, className, sex, channel, pageIndex, pageSize, stateArray);
        apiResponse.setData(objectList);
        return apiResponse;
    }

    /**
     * 导师查询
     *
     * @param searchText 关键词
     * @param orderType  查询方式
     * @param goodAt     擅长类型查询
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @RequestMapping("/queryTutorList")
    @ResponseBody
    public Object queryTutorList(String searchText, String orderType, String goodAt, int pageIndex, int pageSize, String stateArray) throws IOException {
        if(StringUtils.isEmpty(stateArray)){  // 默认是查询发布状态
            stateArray = "1";
        }
        List<Object> objectList = indexService.queryTutorList(searchText, orderType, goodAt, pageIndex, pageSize, stateArray);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        apiResponse.setStatus(apiResponse.SUCCESS);
        apiResponse.setData(objectList);
        return apiResponse;
    }

    /**
     * 查询推荐或置顶的数据
     *
     * @param recommend 推荐(1推荐、0不推荐、2、置顶）
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryRecommend")
    @ResponseBody
    public Object queryRecommend(String recommend, String channelId, Integer pageIndex, Integer pageSize) throws IOException {
        List<Object> objectList = indexService.queryRecommend(recommend, channelId, pageIndex, pageSize);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        apiResponse.setStatus(apiResponse.SUCCESS);
        apiResponse.setData(objectList);
        return apiResponse;
    }

    /**
     * 删除所有索引
     *
     * @return
     */
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Object deleteAll() throws IOException {
        indexService.deleteAllIndex();
        return new APIResponse<>();
    }

    /**
     * 根据id删除索引
     *
     * @param id
     * @param className
     * @return
     */
    @RequestMapping("/deleteById")
    @ResponseBody
    public Object deleteById(String id, String className) throws IOException {
        indexService.deleteById(id, className);
        return new APIResponse<>();
    }

    /**
     * 查询我发表的内容
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryMyPublish")
    @ResponseBody
    public Object queryMyPublish(String searchText, Integer tutorId, String stateArray, String className, Integer pageIndex, Integer pageSize) throws IOException {
        List<Object> objectList = indexService.queryMyPublish(searchText, tutorId, stateArray, className, pageIndex, pageSize);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        apiResponse.setStatus(apiResponse.SUCCESS);
        apiResponse.setData(objectList);
        return apiResponse;
    }

    /**
     * 查询我发表的内容（下面的评论）
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/querySourceComment")
    @ResponseBody
    public Object querySourceComment(Integer tutorId, Integer pageIndex, Integer pageSize) throws IOException {
        List<Object> objectList = indexService.querySourceComment(tutorId, pageIndex, pageSize);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        apiResponse.setStatus(apiResponse.SUCCESS);
        apiResponse.setData(objectList);
        return apiResponse;
    }

    /**
     * 查询我发表的内容（评论的回复）
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("/queryMyComment")
    @ResponseBody
    public Object queryMyComment(String memberId, String baseIdJson) throws IOException {
        List<String> baseIds = JsonUtil.jsonToList(baseIdJson, String.class);

        Map<String, Object> resultMap = indexService.queryMyComment(memberId, baseIds);
        APIResponse<Object> apiResponse = new APIResponse<Object>();
        apiResponse.setStatus(apiResponse.SUCCESS);
        apiResponse.setData(resultMap);
        return apiResponse;
    }


}
